import {
  applyDecorators,
  Body,
  ClassSerializerInterceptor,
  Controller,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  Inject,
  LoggerService,
  NotFoundException,
  Param,
  ParseIntPipe,
  Post,
  Put,
  UseInterceptors,
} from '@nestjs/common';
import { CreateUserDto, UpdateUserDto } from 'src/shared/dtos/user.dto';
import { UserService } from 'src/shared/services/user.service';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { Result } from 'src/shared/vo/result';
import { Logger } from '@nestjs/common';

// import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

@Controller('api/users')
@UseInterceptors(ClassSerializerInterceptor) // 拦截响应值，进行序列化
@ApiTags('api/users') // 标签
export class UserController {
  // @Inject(WINSTON_MODULE_NEST_PROVIDER)
  // private readonly winstonLogger: LoggerService;
  private readonly logger = new Logger(UserController.name);

  constructor(private readonly userService: UserService) {}

  @Post()
  @ApiCreate()
  async create(@Body() createUserDto: CreateUserDto) {
    const res = await this.userService.create(createUserDto);
    return Result.ok(res);
  }

  @Get()
  @ApiFindAll()
  async findAll() {
    const res = await this.userService.findAll();
    return Result.ok(res);
  }

  @Get(':id')
  @ApiFindOne()
  async findOne(@Param('id', ParseIntPipe) id: number) {
    const res = await this.userService.findOne({ where: { id } });
    if (res) {
      return Result.ok(res);
    } else {
      throw new NotFoundException('用户不存在');
    }
  }

  @Put(':id')
  @ApiUpdate()
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    const res = await this.userService.update(id, updateUserDto);
    if (res.affected) {
      return Result.ok(null);
    } else {
      throw new HttpException('用户不存在', HttpStatus.NOT_FOUND);
    }
  }

  @Delete(':id')
  @ApiDelete()
  async delete(@Param('id', ParseIntPipe) id: number) {
    const res = await this.userService.delete(id);
    if (res.affected) {
      return Result.ok(null);
    } else {
      throw new HttpException('用户未找到', HttpStatus.NOT_FOUND);
    }
  }
}

function ApiFindOne() {
  return applyDecorators(
    ApiOperation({ summary: '根据ID，获取某一个用户' }),
    ApiParam({ name: 'id', description: '用户ID', type: 'number' }),
    ApiResponse({ status: 200, description: '成功返回用户信息', type: Result }),
    ApiResponse({ status: 404, description: '用户不存在' }),
  );
}

function ApiCreate() {
  return applyDecorators(
    ApiBearerAuth(),
    ApiOperation({ summary: '创建一个新用户' }), // 接口描述
    ApiBody({ type: CreateUserDto }),
    ApiResponse({ status: 200, description: '创建用户成功', type: Result }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
  );
}

function ApiUpdate() {
  return applyDecorators(
    ApiOperation({ summary: '根据ID，更新某一个用户信息' }),
    ApiBody({ type: UpdateUserDto }),
    ApiResponse({ status: 200, description: '用户信息更新成功', type: Result }),
    ApiResponse({ status: 404, description: '用户不存在' }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
  );
}

function ApiDelete() {
  return applyDecorators(
    ApiOperation({ summary: '根据ID，删除某一个用户' }),
    ApiParam({ name: 'id', description: '用户ID', type: 'number' }),
    ApiResponse({ status: 200, description: '删除成功', type: Result }),
    ApiResponse({ status: 404, description: '用户未找到' }),
  );
}

function ApiFindAll() {
  return applyDecorators(
    ApiOperation({ summary: '获取所有用户列表' }),
    ApiResponse({ status: 200, description: '成功返回用户列表', type: Result }),
  );
}
